package jianzhioffer;

import java.util.HashSet;

public class FindRepeatNumber {

    // 暴力
    public int findRepeatNumber(int[] nums) {
        int res = -1;
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (set.contains(num)) {
                return num;
            }
            set.add(num);
        }
        return res;
    }

    // 交换元素
    public int findRepeatNumber2(int[] nums) {
        int index=0;
        while (index<nums.length){
            if (nums[index]==index){
                index++;
                continue;
            }else if (nums[nums[index]]==nums[index]){
                return nums[index];
            }
            int t = nums[index];
            nums[index] = nums[nums[index]];
            nums[t] = t;
        }
        return -1;
    }
}
